
package com.atguign.gulimall.ware.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Slf4j
@Configuration
public class MyRabbitConfig {
    /**
     * rabbitmq序列化方式，使用json方式序列化，消息会被序列化成json
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    //交换机配置
    @Bean
    public Exchange stockEventExchange(){
        return new TopicExchange("stock-event-exchange",true,false);
    }
    //队列配置
    @Bean
    public Queue stockReleaseStockQueue(){
        return new Queue("stock.release.stock.queue",true,false,false);
    }
    @Bean
    public Queue stockDelayQueue(){
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("x-dead-letter-exchange","stock-event-exchange");
        stringObjectHashMap.put("x-dead-letter-routing-key","stock.release");
        stringObjectHashMap.put("x-message-ttl",60000*10);
        return new Queue("stock.delay.stock.queue",true,false,false,stringObjectHashMap);
    }
    //绑定
    @Bean
    public Binding stockReleaseBinding(){
        return  new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE,"stock-event-exchange","stock.release.#",null);
    }
    @Bean
    public Binding stockLockedBinding(){
        return  new Binding("stock.delay.stock.queue", Binding.DestinationType.QUEUE,"stock-event-exchange","stock.locked",null);
    }


}